<template>
    <div class="prodetail" v-if="goodsObj.goods_name">

        <!-- 顶部navbar -->
        <van-nav-bar fixed title="商品详情页" left-arrow @click-left="$router.go(-1)" />

        <!-- 这是轮播图 -->
        <van-swipe :autoplay="3000" @change="onChange">
            <van-swipe-item v-for="(image) in goodsObj.goods_images" :key="image.file_id">
                <img :src="image.external_url" />
            </van-swipe-item>
            <template #indicator>
                <div class="custom-indicator">{{ current + 1 }} / {{ goodsObj.goods_images.length }}</div>
            </template>
        </van-swipe>

        <!-- 商品说明 -->
        <div class="info">
            <div class="title">
                <div class="price">
                    <span class="now">￥{{ goodsObj.goods_price_min }}</span>
                    <span class="oldprice">￥{{ goodsObj.goods_price_max }}</span>
                </div>
                <div class="sellcount">已售{{ goodsObj.goods_sales }}件</div>
            </div>
            <div class="msg text-ellipsis-2">{{ goodsObj.goods_name }}</div>

            <div class="service">
                <div class="left-words">
                    <span><van-icon name="passed" />七天无理由退货</span>
                    <span><van-icon name="passed" />48小时发货</span>
                </div>
                <div class="right-icon">
                    <van-icon name="arrow" />
                </div>
            </div>
        </div>

        <!-- 商品评价 -->
        <div class="comment" v-if="total > 0">
            <div class="comment-title">
                <div class="left">商品评价 ({{ total }}条)</div>
                <div class="right">查看更多 <van-icon name="arrow" /> </div>
            </div>
            <div class="comment-list">
                <div class="comment-item" v-for="item in commentList" :key="item.comment_id">
                    <div class="top">
                        <img :src="item.user.avatar_url" alt="">
                        <div class="name">{{ item.user.nick_name }}</div>
                        <van-rate :size="16" :value="item.score / 2" color="#ffd21e" void-icon="star" void-color="#eee" />
                    </div>
                    <div class="content">{{ item.content }}</div>
                    <div class="time">{{ item.create_time }}</div>
                </div>
            </div>
        </div>

        <!-- 商品描述 -->
        <div class="desc">
            <div class="desc" v-html="goodsObj.content"></div>
        </div>

        <!-- 底部 -->
        <!-- <div class="footer">
            <div class="icon-home" @click="$router.push('/')" >
                <van-icon name="wap-home-o" />
                <span>首页</span>
            </div>
            <div class="icon-cart">
                <van-icon name="shopping-cart-o" />
                <span>购物车</span>
            </div>
            <div class="btn-add" @click="clickShoppingCart('cart')">加入购物车</div>
            <div class="btn-buy"  @click="clickShoppingCart('buy')">立刻购买</div>
        </div> -->
        <div class="footer-box-contain">
            <van-goods-action>
                <van-goods-action-icon icon="shop-o" text="首页" @click="$router.push('/')" />
                <van-goods-action-icon icon="cart-o" text="购物车" :badge="cartTotal > 0 ? cartTotal : ''" />
                <van-goods-action-button type="warning" text="加入购物车" @click="clickShoppingCart('cart')" />
                <van-goods-action-button type="danger" text="立即购买" @click="clickShoppingCart('buy')" />
            </van-goods-action>
        </div>


        <!-- 自定义面板 -->
        <van-action-sheet v-model="showPannel" :title="model === 'cart' ? '加入购物车' : '立即购买'">
              <div class="product">
        <div class="product-title">
          <div class="left">
            <img :src="goodsObj.goods_image" alt="">
          </div>
          <div class="right">
            <div class="price">
              <span>¥</span>
              <span class="nowprice">{{ goodsObj.goods_price_min }}</span>
            </div>
            <div class="count">
              <span>库存</span>
              <span>{{ goodsObj.stock_total }}</span>
            </div>
          </div>
        </div>
        <div class="num-box">
          <span>数量</span>
          <count-box v-model="addCount"></count-box>
        </div>
        <div class="showbtn" v-if="goodsObj.stock_total > 0">
          <div class="btn" v-if="model === 'cart'" @click="addCart">加入购物车</div>
          <div class="btn now" v-else @click="goBuyNow">立刻购买</div>
        </div>
        <div class="btn-none" v-else>该商品已抢完</div>
      </div>
        </van-action-sheet>
    </div>
</template>

<script>
import { getProDetail, getProComments } from '@/api/product/index'
import { addCart } from '@/api/cart/index'
import CountBox from '@/components/CountBox/index'
import loginConfirm from '@/mixins/loginConfirm.js'
export default {
    name: 'ProDetail',
    mixins: [loginConfirm],
    components: {
        CountBox
    },
    data() {
        return {
            images: [
                'https://img01.yzcdn.cn/vant/apple-1.jpg',
                'https://img01.yzcdn.cn/vant/apple-2.jpg'
            ],
            current: 0,

            // 商品对象
            goodsObj: {},
            total: 0,
            commentList: [],
            // defaultImg

            // 显示与隐藏
            showPannel: false,
            // 区分购物车还是立即购买
            model: 'cart',
            // 购买商品数量
            addCount: 1,
            // 购物车的数量
            cartTotal: 0
            
        }
    },
    computed: {
        goods_id() { 
            return this.$route.params.id
        }
    },
    created() {
        this.getProDetail()
        this.getComments()
     },
    methods: {
        onChange(index) {
            this.current = index
        },

        // 获取商品详情页
        async getProDetail() { 
            const { data: { detail } } = await getProDetail({ goodsId : this.goods_id })
            this.goodsObj = detail || {}
        },

        // 获取商品评价
        async getComments() {
            const { data: { list, total } } = await getProComments(this.goods_id, 3)
            this.commentList = list
            this.total = total
        },

        // 点击购物车
        clickShoppingCart(type) { 
            this.showPannel = true
            this.model = type
        },

        // 加入购物车
        async addCart() {
            // 只有登录后才能加入购物车,没有登录不能加入购物车
            // 判断token是否存在, 获取token
            // this.$store.getters.token

            
            if (this.loginConfirm()) {
                return
            }

            // if (!this.$store.getters.token) {
            //     // 不存在
            //     this.$dialog.confirm({
            //         title: '温馨提示',
            //         message: '此时需要登录才能操作哦',
            //         cancelButtonText: '再逛逛',
            //         confirmButtonText: '去登录'
            //     }).then(() => { 
            //         // 跳转到登录页, push会留下历史记录,返回的时候,使用replace,不会留下历史记录
            //         this.$router.replace({
            //             path: '/login',
            //             query: {
            //                 backUrl: this.$route.fullPath
            //             }
            //         })
            //     }).catch(() => { })
            //     return 
            // }
            // 存在
              const { data } = await addCart(this.goods_id, this.addCount, this.goodsObj.skuList[0].goods_sku_id)
                this.cartTotal = data.cartTotal
                this.$toast('加入购物车成功')
                this.showPannel = false
                this.$router.push('/cart')
        },


        // 立即购买
        goBuyNow() {
            if (this.loginConfirm()) {
                return
            }
            this.$router.push({
                path: '/pay',
                query: {
                    mode: 'buyNow',
                    goodsId: this.goods_id,
                    goodsSkuId: this.goodsObj.skuList[0].goods_sku_id,
                    goodsNum: this.addCount
                }
            })
        }

        // 加入购物车的商品
    }
}
</script>

<style lang="less" scoped>
.prodetail {
    padding-top: 46px;
    padding-bottom: 50px;

    ::v-deep .van-icon-arrow-left {
        color: #333;
    }

    img {
        display: block;
        width: 100%;
    }

    .custom-indicator {
        position: absolute;
        right: 10px;
        bottom: 10px;
        padding: 5px 10px;
        font-size: 12px;
        background: rgba(0, 0, 0, 0.1);
        border-radius: 15px;
    }

    .desc {
        width: 100%;
        overflow: scroll;

        ::v-deep img {
            display: block;
            width: 100% !important;
        }
    }

    .info {
        padding: 10px;
    }

    .title {
        display: flex;
        justify-content: space-between;

        .now {
            color: #fa2209;
            font-size: 20px;
        }

        .oldprice {
            color: #959595;
            font-size: 16px;
            text-decoration: line-through;
            margin-left: 5px;
        }

        .sellcount {
            color: #959595;
            font-size: 16px;
            position: relative;
            top: 4px;
        }
    }

    .msg {
        font-size: 16px;
        line-height: 24px;
        margin-top: 5px;
    }

    .service {
        display: flex;
        justify-content: space-between;
        line-height: 40px;
        margin-top: 10px;
        font-size: 16px;
        background-color: #fafafa;

        .left-words {
            span {
                margin-right: 10px;
            }

            .van-icon {
                margin-right: 4px;
                color: #fa2209;
            }
        }
    }

    .comment {
        padding: 10px;
    }

    .comment-title {
        display: flex;
        justify-content: space-between;

        .right {
            color: #959595;
        }
    }

    .comment-item {
        font-size: 16px;
        line-height: 30px;

        .top {
            height: 30px;
            display: flex;
            align-items: center;
            margin-top: 20px;

            img {
                width: 20px;
                height: 20px;
            }

            .name {
                margin: 0 10px;
            }
        }

        .time {
            color: #999;
        }
    }

    .footer {
        position: fixed;
        left: 0;
        bottom: 0;
        width: 100%;
        height: 55px;
        background-color: #fff;
        border-top: 1px solid #ccc;
        display: flex;
        justify-content: space-evenly;
        align-items: center;

        .icon-home,
        .icon-cart {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            font-size: 14px;

            .van-icon {
                font-size: 24px;
            }
        }

        .btn-add,
        .btn-buy {
            height: 36px;
            line-height: 36px;
            width: 120px;
            border-radius: 18px;
            background-color: #ffa900;
            text-align: center;
            color: #fff;
            font-size: 14px;
        }

        .btn-buy {
            background-color: #fe5630;
        }
    }
}

.tips {
    padding: 10px;
}
.product {
  .product-title {
    display: flex;
    .left {
      img {
        width: 90px;
        height: 90px;
      }
      margin: 10px;
    }
    .right {
      flex: 1;
      padding: 10px;
      .price {
        font-size: 14px;
        color: #fe560a;
        .nowprice {
          font-size: 24px;
          margin: 0 5px;
        }
      }
    }
  }

  .num-box {
    display: flex;
    justify-content: space-between;
    padding: 10px;
    align-items: center;
  }

  .btn, .btn-none {
    height: 40px;
    line-height: 40px;
    margin: 20px;
    border-radius: 20px;
    text-align: center;
    color: rgb(255, 255, 255);
    background-color: rgb(255, 148, 2);
  }
  .btn.now {
    background-color: #fe5630;
  }
  .btn-none {
    background-color: #cccccc;
  }
}
</style>